#include <Arduino.h>

#if defined(CH551) || defined(CH552)
uint16_t digitalReadDirectLutAddrAndCarryReturn(uint8_t pin) {
  //'dpl' (LSB),'dph','b' & 'acc'
  pin;
  // clang-format off
  __asm__(
    ";setb and return are 2 bytes, mul by 3       \n"
    "    mov a,dpl                                \n"
    "    add a,dpl                                \n"
    "    add a,dpl                                \n"
    "    mov dptr,#direct_read_carry_lut$         \n"
    "    add a,dpl                                \n"
    "    mov dpl,a                                \n"
    "    clr a                                    \n"
    "    addc a,dph                               \n"
    "    mov dph,a                                \n"
    "    clr a                                    \n"
    "    jmp @a+dptr                              \n"

    "direct_read_carry_lut$:                      \n"
    ";No PORT0                                    \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    
    ";PORT1                                       \n"
    "    mov	c,_P1_0                             \n"
    "    ret                                      \n"
    "    mov	c,_P1_1                             \n"
    "    ret                                      \n"
    "    mov	c,_P1_2                             \n"
    "    ret                                      \n"
    "    mov	c,_P1_3                             \n"
    "    ret                                      \n"
    "    mov	c,_P1_4                             \n"
    "    ret                                      \n"
    "    mov	c,_P1_5                             \n"
    "    ret                                      \n"
    "    mov	c,_P1_6                             \n"
    "    ret                                      \n"
    "    mov	c,_P1_7                             \n"
    "    ret                                      \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    
    ";No PORT2                                    \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"

    ";PORT3                                       \n"
    "    mov	c,_P3_0                             \n"
    "    ret                                      \n"
    "    mov	c,_P3_1                             \n"
    "    ret                                      \n"
    "    mov	c,_P3_2                             \n"
    "    ret                                      \n"
    "    mov	c,_P3_3                             \n"
    "    ret                                      \n"
    "    mov	c,_P3_4                             \n"
    "    ret                                      \n"
    "    mov	c,_P3_5                             \n"
    "    ret                                      \n"
    "    mov	c,_P3_6                             \n"
    "    ret                                      \n"
    "    mov	c,_P3_7                             \n"
    "    ret                                      \n"
    "    nop \n nop \n ret                        \n"
    "    nop \n nop \n ret                        \n"    
  );
  // clang-format on
}

#endif
